<template>
  <view class="article-section" :style="{'margin-left': item.bgMargin + 'px', 'margin-right': item.bgMargin + 'px', 'margin-top': item.pageMargin + 'px'}">
    <view class="section-header">
      <text class="section-title">精选文章</text>
    </view>
    <view class="article-list">
      <view
        v-for="(article, index) in articleData"
        :key="index"
        class="article-item"
        @click="goArticle(article.id)"
      >
        <view class="article-image">
          <image :src="article.img ? article.img : defaultImg" mode="widthFix"></image>
        </view>
        <view class="article-info">
          <text class="article-title">{{ article.title }}</text>
          <text class="article-desc">{{ article.desc }}</text>
          <view class="article-meta">
            <text class="article-date">{{ article.create_time }}</text>
            <text class="article-views">{{ article.views }} 阅读</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import { BASE_URL } from '@/config/app';
export default {
	name: "news",
	props: {
		item: {
			type: Object,
			default() {
				return {}
			}
		}
	},
	data() {
		return {
			articleData: [],
			defaultImg: BASE_URL + '/static/home/default/image/default.png'
		}
	},
	mounted() {
		this.getArticleList(this.item)
	},
	methods: {
		// 文章详情
		goArticle(id) {
			uni.navigateTo({
				url: '/pages/article/detail?id=' + id
			})
		},
		// 获取文章信息
		async getArticleList(row) {
			let res = await this.$api.home.getArticleList.get({limit: row.num, cate_id: row.cate_id})
			this.articleData = res.data
		},
	}
}
</script>

<style scoped lang="scss">
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.section-title {
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 12px;
  color: #333;
  padding-top: 20rpx;
}

.dark-theme .section-title {
  color: #fff;
}

.more-btn {
  color: #999;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
}

/* 精选文章 */
.article-section {
  padding: 16px;
  background-color: #fff;
  border-radius: 8px;
}

.dark-theme .article-section {
  background-color: #2a2a2a;
}

.article-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  
  /* 确保内容正确包裹 */
  overflow: hidden;
}

.article-item {
  display: flex;
  gap: 12px;
  cursor: pointer;
}

.article-image {
  width: 80px;
  height: 80px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}

.article-info {
  flex: 1;
}

.article-title {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 4px;
  color: #333;
}

.dark-theme .article-title {
  color: #fff;
}

.article-desc {
  font-size: 12px;
  color: #666;
  margin-bottom: 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.dark-theme .article-desc {
  color: #aaa;
}

.article-meta {
  display: flex;
  gap: 12px;
}

.article-date, .article-views {
  font-size: 12px;
  color: #999;
}
</style>